package interview.hw_od;

import java.util.*;
import java.util.stream.Collectors;

/**
 * AI面板识别 https://codefun2000.com/p/P1326
 */
public class AiPanel {

    /**
     * 思路：先获取所有灯 然后按照中线高度升序排序，然后循环：取出当前最高点，对比中线（yMid - m <= halfH）取出当前层所有元素，
     *      取完后将当前层list按照id升序排序然后addAll到结果list，然后再继续扫描下一层
     * @param args
     */
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = Integer.parseInt(sc.nextLine());
        List<Light> list = new ArrayList<>(n);
        for (int i = 1;i <= n;i++) {
            list.add(new Light(sc.nextLine()));
        }
        int halfH = list.get(0).yMid - list.get(0).y1;
        //按照中线排序，中线越小越高
        list.sort((l1, l2) -> l1.yMid - l2.yMid);
        int i = 0;
        List<Light> result = new ArrayList<>();
        while (i < list.size()) {
            //先获取当前最高点，然后从队列中比较获取当前层元素
            List<Light> level = new ArrayList<>();
            int m = list.get(i).yMid;
            while (i < list.size() && list.get(i).yMid - m <= halfH)
                level.add(list.get(i++));
            //获取当前层所有元素后按照x1坐标升序排序，然后加入结果集
            level.sort((l1, l2) -> Integer.compare(l1.x1, l2.x1));
            result.addAll(level);
        }
        System.out.println(result.stream().map(x -> x.id).collect(Collectors.joining(" ")));
    }

    static class Light {
        public String id;
        public int x1;
        public int y1;
        public int yMid;

        public Light(String str) {
            String[] ss = str.split(" ");
            id = ss[0];
            x1 = Integer.parseInt(ss[1]);
            y1 = Integer.parseInt(ss[2]);
            yMid = (Integer.parseInt(ss[2]) + Integer.parseInt(ss[4])) / 2;
        }
    }
}
